package com.lazycat.backend.service.impl.shop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lazycat.backend.mapper.CommentMapper;
import com.lazycat.backend.mapper.GoodMapper;
import com.lazycat.backend.mapper.StoreMapper;
import com.lazycat.backend.mapper.UserMapper;
import com.lazycat.backend.pojo.Comment;
import com.lazycat.backend.pojo.Good;
import com.lazycat.backend.pojo.Store;
import com.lazycat.backend.pojo.User;
import com.lazycat.backend.service.shop.FetchSingleGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class FetchSingleGoodServiceImpl implements FetchSingleGoodService {
    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public JSONObject fetch(Integer id) {
        QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
        goodQueryWrapper.eq("id", id);
        Good good = goodMapper.selectOne(goodQueryWrapper);
        JSONObject resp = new JSONObject();
        // Good 的基本信息
        resp.put("error_message", "success");
        resp.put("name", good.getGoodName());
        resp.put("img", good.getGoodImg());
        resp.put("price", good.getGoodPrice());
        resp.put("id", good.getId());
        resp.put("description", good.getGoodText());

        // Good的Owner信息
        Integer storeId = good.getStoreId();
        QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
        storeQueryWrapper.eq("id", storeId);
        Store store = storeMapper.selectOne(storeQueryWrapper);
        Integer ownerId = store.getUserId();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id", ownerId);
        User owner = userMapper.selectOne(userQueryWrapper);
        resp.put("ownerName", owner.getUsername());
        resp.put("seller_id", ownerId);

        //评论
        ArrayList<JSONObject> commentItems = new ArrayList<>();
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("good_id", good.getId());
        List<Comment> comments = commentMapper.selectList(commentQueryWrapper);
        for (Comment comment : comments) {
            JSONObject item = new JSONObject();
            Integer commentatorId = comment.getCommentatorId();
            System.out.println(commentatorId);
            QueryWrapper<User> _userQueryWrapper = new QueryWrapper<>();
            _userQueryWrapper.eq("id", commentatorId);
            User commentator = userMapper.selectOne(_userQueryWrapper);
            item.put("userImg", commentator.getImg());
            item.put("userName", commentator.getUsername());
            item.put("content", comment.getText());
            commentItems.add(item);
        }
        resp.put("comments", commentItems);
        return resp;
    }
}
